#include "stdafx.hpp"
#include "ColligereColligereXmlDataImporter.hpp"

void col::ColligereXmlDataImporter::Load ( const wxURI &rIn )
{
	wxASSERT ( rIn.GetScheme() == "file" );
	const wxString in_file ( wxURI ( rIn.GetPath() ).BuildUnescapedURI() );

	if ( !mInDoc.Load ( in_file ) )
	{
		throw DataImporterError ( rIn, _("Unable to load the input file.") );
	}
	if ( !IsValidColligereXmlFile() )
	{
		throw DataImporterError ( rIn, _("The input file is not a valid Colligere XML-document." ) );
	}
}

wxUint32 col::ColligereXmlDataImporter::GetItemCount() const
{
	return XmlUtils::GetItemCount ( mInDoc, "album" );
}

bool col::ColligereXmlDataImporter::IsValidColligereXmlFile() const
{
	wxLogError ( mInDoc.GetRoot()->GetName() );
	return mInDoc.IsOk() && mInDoc.GetRoot()->GetName() == "collection";
}

void col::ColligereXmlDataImporter::Import ( DatabaseInterfaceBase &rDest )
{
	using namespace boost;

	DataImporterEvent evt_import_start ( COLLIGERE_EVT_IMPORT_START, this );

	rDest.GetLayout()->ProcessEvent ( evt_import_start );

	foreach ( const wxXmlNode *ar, FindNodes ( mInDoc.GetRoot(), "artist" ) )
	{
		Artist artist;
		InitializeItemFromNode ( ar, artist );

		foreach ( const wxXmlNode *an, FindNodes ( ar, "album") )
		{
			Album album;
			InitializeItemFromNode ( an, album );

			foreach ( const wxXmlNode *t, FindNodes ( an, "track" ) )
			{
				Track track;
				InitializeItemFromNode ( t, track );
				album.AddChild ( track );
			}

			artist.AddChild ( album );

			DataImporterEvent evt_item_imported ( COLLIGERE_EVT_ITEM_IMPORTED, 
				this, &album );
			rDest.GetLayout()->ProcessEvent ( evt_item_imported );
		}
		rDest.GetHandler()->AddItem ( artist );
	}

	DataImporterEvent evt_import_end ( COLLIGERE_EVT_IMPORT_END, this );
	rDest.GetLayout()->ProcessEvent ( evt_import_end );
}

void col::ColligereXmlDataImporter::InitializeItemFromNode
( const wxXmlNode *pNode, DatabaseItemBase &rItem ) const
{
	if ( Artist *artist = dynamic_cast < Artist * > ( &rItem ) )
	{
		artist->SetArtist ( GetNodeContent ( pNode, "name" ) );
		artist->SetCountry ( GetNodeContent ( pNode, "country" ) );
		artist->SetHomepage ( GetNodeContent ( pNode, "homepage" ) );
	}
	else if ( Album *album = 
		dynamic_cast < Album * > ( &rItem ) )
	{
		album->SetAlbum ( GetNodeContent ( pNode, "title") ).
			SetGenre ( GetNodeContent ( pNode, "genre") ).
			SetFormat ( GetNodeContent ( pNode, "format") ).
			SetStatus ( GetNodeContent ( pNode, "status") ).
			SetExtInfo ( GetNodeContent ( pNode, "ext_info") ).
			SetLabel ( GetNodeContent ( pNode, "label") ).
			SetCatalogNum ( GetNodeContent ( pNode, "catalog_num") ).
			SetRating ( GetNodeContent ( pNode, "rating") ).
			SetSellPrice ( GetNodeContent ( pNode, "sell_price") ).
			SetPurchasePrice ( GetNodeContent ( pNode, "purchase_price") ).
			SetCondition ( GetNodeContent ( pNode, "condition") ).
			SetYear ( GetNodeContent ( pNode, "year") );
	}
	else if ( Track *track = 
		dynamic_cast < Track * > ( &rItem ) )
	{
		track->SetNum ( GetNodeContent ( FindNode ( pNode, "num") ) );
		track->SetTitle ( GetNodeContent ( FindNode ( pNode, "title") ) );		
		track->SetLength ( GetNodeContent ( FindNode ( pNode, "length") ) );
	}
}
